package com.cskaoyan.controller;

import com.cskaoyan.aspectj.admin.AdminCreate;
import com.cskaoyan.aspectj.admin.AdminDelete;
import com.cskaoyan.aspectj.admin.AdminUpdate;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminAdminCreateVo;
import com.cskaoyan.bean.vo.AdminAdminUpdateVo;
import com.cskaoyan.service.AdminServiceImpl;
import com.cskaoyan.utils.DateToString;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 管理员管理
 * @Author: JianhuaZeng
 * @Date: 2022/12/28 21:40
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminServiceImpl adminService;

    /**
     * @description: 获取管理员信息列表
     * @param: pageInfo
     * @param: username
     * @return: com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.common.CommonData>
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @GetMapping("list")
    public BaseRespVo<CommonData> list(BasePageInfo pageInfo, String username){
        val commonData = adminService.list(pageInfo, username);
        return BaseRespVo.ok(commonData,"成功");
    }

    /**
     * @description: 新增管理员账号
     * @param: marketAdmin
     * @return: com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.AdminAdminCreateVo>
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @AdminCreate
    @PostMapping("create")
    public BaseRespVo<AdminAdminCreateVo> create(@RequestBody MarketAdmin marketAdmin){

        MarketAdmin admin = adminService.addAdmin(marketAdmin);
        // 构造返回对象
        AdminAdminCreateVo adminCreateVo = new AdminAdminCreateVo();
        adminCreateVo.setId(admin.getId());
        adminCreateVo.setUsername(admin.getUsername());
        adminCreateVo.setPassword(admin.getPassword());
        adminCreateVo.setAvatar(admin.getAvatar());
        adminCreateVo.setAddTime(DateToString.getDateString(admin.getAddTime()));
        adminCreateVo.setUpdateTime(DateToString.getDateString(admin.getUpdateTime()));
        adminCreateVo.setRoleIds(admin.getRoleIds());
        return BaseRespVo.ok(adminCreateVo,"成功");
    }

    /**
     * @description: 修改管理员账号
     * @param: marketAdmin
     * @return: com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.AdminAdminUpdateVo>
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @AdminUpdate
    @PostMapping("update")
    public BaseRespVo<AdminAdminUpdateVo> update(@RequestBody MarketAdmin marketAdmin){
        Date date = new Date();
        marketAdmin.setUpdateTime(date);
        adminService.modify(marketAdmin);

        // 构造返回对象
        AdminAdminUpdateVo adminUpdateVo = new AdminAdminUpdateVo();
        adminUpdateVo.setId(marketAdmin.getId());
        adminUpdateVo.setUsername(marketAdmin.getUsername());
        adminUpdateVo.setAvatar(marketAdmin.getAvatar());
        adminUpdateVo.setUpdateTime(DateToString.getDateString(date));
        adminUpdateVo.setRoleIds(marketAdmin.getRoleIds());
        return BaseRespVo.ok(adminUpdateVo,"成功");
    }

    /**
     * @description: 逻辑删除
     * @param: id
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @AdminDelete
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        // 逻辑删除
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId((Integer) map.get("id"));
        marketAdmin.setDeleted(true);
        adminService.modify(marketAdmin);
        return BaseRespVo.ok("成功");
    }
}
